home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c15 / CGITools.h next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  6.3 KB  |  189 lines

  1. //: CGITools.h
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Automatically extracts and decodes data
  50. // from CGI GETs and POSTs. Tested with GNU C++ 
  51. // (available for most server machines).
  52. #include <string.h>
  53. #include <vector> // STL vector
  54. using namespace std;
  55.  
  56. // A class to hold a single name-value pair from
  57. // a CGI query. CGI_vector holds Pair objects and
  58. // returns them from its operator[].
  59. class Pair {
  60.   char* nm;
  61.   char* val;
  62. public:
  63.   Pair() { nm = val = 0; }
  64.   Pair(char* name, char* value) {
  65.     // Creates new memory:
  66.     nm = decodeURLString(name);
  67.     val = decodeURLString(value);
  68.   }
  69.   const char* name() const { return nm; }
  70.   const char* value() const { return val; }
  71.   // Test for "emptiness"
  72.   bool empty() const {
  73.     return (nm == 0) || (val == 0);
  74.   }
  75.   // Automatic type conversion for boolean test:
  76.   operator bool() const {
  77.     return (nm != 0) && (val != 0);
  78.   }
  79.   // The following constructors & destructor are
  80.   // necessary for bookkeeping in C++.
  81.   // Copy-constructor:
  82.   Pair(const Pair& p) {
  83.     if(p.nm == 0 || p.val == 0) {
  84.       nm = val = 0;
  85.     } else {
  86.       // Create storage & copy rhs values:
  87.       nm = new char[strlen(p.nm) + 1];
  88.       strcpy(nm, p.nm);
  89.       val = new char[strlen(p.val) + 1];
  90.       strcpy(val, p.val);
  91.     }
  92.   }
  93.   // Assignment operator:
  94.   Pair& operator=(const Pair& p) {
  95.     // Clean up old lvalues:
  96.     delete nm;
  97.     delete val;
  98.     if(p.nm == 0 || p.val == 0) {
  99.       nm = val = 0;
  100.     } else {
  101.       // Create storage & copy rhs values:
  102.       nm = new char[strlen(p.nm) + 1];
  103.       strcpy(nm, p.nm);
  104.       val = new char[strlen(p.val) + 1];
  105.       strcpy(val, p.val);
  106.     }
  107.     return *this;
  108.   } 
  109.   ~Pair() { // Destructor
  110.     delete nm; // 0 value OK
  111.     delete val;
  112.   }
  113.   // If you use this method outide this class, 
  114.   // you're responsible for calling 'delete' on
  115.   // the pointer that's returned:
  116.   static char* 
  117.   decodeURLString(const char* URLstr) {
  118.     int len = strlen(URLstr);
  119.     char* result = new char[len + 1];
  120.     memset(result, len + 1, 0);
  121.     for(int i = 0, j = 0; i <= len; i++, j++) {
  122.       if(URLstr[i] == '+')
  123.         result[j] = ' ';
  124.       else if(URLstr[i] == '%') {
  125.         result[j] =
  126.           translateHex(URLstr[i + 1]) * 16 +
  127.           translateHex(URLstr[i + 2]);
  128.         i += 2; // Move past hex code
  129.       } else // An ordinary character
  130.         result[j] = URLstr[i];
  131.     }
  132.     return result;
  133.   }
  134.   // Translate a single hex character; used by
  135.   // decodeURLString():
  136.   static char translateHex(char hex) {
  137.     if(hex >= 'A')
  138.       return (hex & 0xdf) - 'A' + 10;
  139.     else
  140.       return hex - '0';
  141.   }
  142. };
  143.  
  144. // Parses any CGI query and turns it
  145. // into an STL vector of Pair objects:
  146. class CGI_vector : public vector<Pair> {
  147.   char* qry;
  148.   const char* start; // Save starting position
  149.   // Prevent assignment and copy-construction:
  150.   void operator=(CGI_vector&);
  151.   CGI_vector(CGI_vector&);
  152. public:
  153.   // const fields must be initialized in the C++
  154.   // "Constructor initializer list":
  155.   CGI_vector(char* query) :
  156.       start(new char[strlen(query) + 1]) {
  157.     qry = (char*)start; // Cast to non-const
  158.     strcpy(qry, query);
  159.     Pair p;
  160.     while((p = nextPair()) != 0)
  161.       push_back(p);
  162.   }
  163.   // Destructor:
  164.   ~CGI_vector() { delete start; }
  165. private:
  166.   // Produces name-value pairs from the query 
  167.   // string. Returns an empty Pair when there's 
  168.   // no more query string left:
  169.   Pair nextPair() {
  170.     char* name = qry;
  171.     if(name == 0 || *name == '\0')
  172.       return Pair(); // End, return null Pair
  173.     char* value = strchr(name, '=');
  174.     if(value == 0)
  175.       return Pair(); // Error, return null Pair
  176.     // Null-terminate name, move value to start
  177.     // of its set of characters:
  178.     *value = '\0';
  179.     value++;
  180.     // Look for end of value, marked by '&':
  181.     qry = strchr(value, '&');
  182.     if(qry == 0) qry = ""; // Last pair found
  183.     else {
  184.       *qry = '\0'; // Terminate value string
  185.       qry++; // Move to next pair
  186.     }
  187.     return Pair(name, value);
  188.   }
  189. }; ///:~